package org.example.diet_compass.resp;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.experimental.Accessors;

/**
 * @author RoselleShaw
 * @create 2024-03-22 16:34
 */
@Data
// 开启链式编程
@Accessors(chain = true)
@Schema(description = "响应结果实体类")
public class Result<T> {

    @Schema(description = "业务状态码")
    private Integer code;
    @Schema(description = "消息")
    private String message;
    @Schema(description = "数据对象")
    private T data;
    @Schema(description = "时间戳")
    private Long timestamp;

    private Result() {
        this.timestamp = System.currentTimeMillis();
    }

    public static <T> Result<T> build(T data, Integer code, String message) {
        return new Result<T>()
                .setCode(code)
                .setMessage(message)
                .setData(data);
    }

    public static <T> Result<T> build(T data, ResultCodeEnum returnCodeEnum) {
        return new Result<T>()
                .setCode(returnCodeEnum.getCode())
                .setMessage(returnCodeEnum.getMessage())
                .setData(data);
    }

}
